<?php
namespace Admin\Model;
use Think\Model\RelationModel;
class UserAddressModel extends RelationModel
{
	protected $insertFields = array('id','member_id','consignee','province','city','district','address','zipcode','mobile','is_default');
	protected $updateFields = array('id','member_id','consignee','province','city','district','address','zipcode','mobile','is_default');

    protected $_link = array(
        array(
            'class_name' => 'Region',
            'mapping_type' => self::HAS_ONE,
            'mapping_key' => 'province',
            'foreign_key' => 'region_id',
            'mapping_name' => 'province',
        ),
        array(
            'class_name' => 'Region',
            'mapping_type' => self::HAS_ONE,
            'mapping_key' => 'city',
            'foreign_key' => 'region_id',
            'mapping_name' => 'city',
        ),
        array(
            'class_name' => 'Region',
            'mapping_type' => self::HAS_ONE,
            'mapping_key' => 'district',
            'foreign_key' => 'region_id',
            'mapping_name' => 'district',
        )
    );

	protected $_validate = array(
//		array('member_id', 'require', '会员id不能为空！', 1, 'regex', 3),
//		array('member_id', 'number', '会员id必须是一个整数！', 1, 'regex', 3),
		array('consignee', 'require', '收货人地址不能为空！', 1, 'regex', 3),
		array('consignee', '1,60', '收货人地址的值最长不能超过 60 个字符！', 1, 'length', 3),
		array('province', 'require', '省份不能为空！', 1, 'regex', 3),
		array('province', 'number', '省份必须是一个整数！', 1, 'regex', 3),
		array('city', 'require', '城市不能为空！', 1, 'regex', 3),
		array('city', 'number', '城市必须是一个整数！', 1, 'regex', 3),
		array('district', 'require', '地区不能为空！', 1, 'regex', 3),
		array('district', 'number', '地区必须是一个整数！', 1, 'regex', 3),
		array('address', 'require', '详细地址不能为空！', 1, 'regex', 3),
		array('address', '1,120', '详细地址的值最长不能超过 120 个字符！', 1, 'length', 3),
		array('zipcode', '1,60', '邮编的值最长不能超过 60 个字符！', 2, 'length', 3),
		array('mobile', 'require', '手机号不能为空！', 1, 'regex', 3),
		array('mobile', '1,60', '手机号的值最长不能超过 60 个字符！', 1, 'length', 3),
		array('is_default', 'number', '默认发货地址必须是一个整数！', 2, 'regex', 3),
	);
	public function search($pageSize = 20)
	{
		/**************************************** 搜索 ****************************************/
		$where = array();
		if($member_id = I('get.member_id'))
			$where['member_id'] = array('eq', $member_id);
		if($consignee = I('get.consignee'))
			$where['consignee'] = array('like', "%$consignee%");
		if($province = I('get.province'))
			$where['province'] = array('eq', $province);
		if($city = I('get.city'))
			$where['city'] = array('eq', $city);
		if($district = I('get.district'))
			$where['district'] = array('eq', $district);
		if($address = I('get.address'))
			$where['address'] = array('like', "%$address%");
		if($zipcode = I('get.zipcode'))
			$where['zipcode'] = array('like', "%$zipcode%");
		if($mobile = I('get.mobile'))
			$where['mobile'] = array('like', "%$mobile%");
		if($addtime = I('get.addtime'))
			$where['addtime'] = array('like', "%$addtime%");
		if($is_default = I('get.is_default'))
			$where['is_default'] = array('eq', $is_default);
		/************************************* 翻页 ****************************************/
		$count = $this->alias('a')->where($where)->count();
		$page = new \Think\Page($count, $pageSize);
		// 配置翻页的样式
		$page->setConfig('prev', '上一页');
		$page->setConfig('next', '下一页');
		$data['page'] = $page->show();
		/************************************** 取数据 ******************************************/
		$data['data'] = $this->relation(true)->alias('a')->where($where)->group('a.id')->limit($page->firstRow.','.$page->listRows)->select();
		return $data;
	}
	// 添加前
	protected function _before_insert(&$data, $option)
	{
	    $data['member_id'] = session('m_id');
        $data['addtime'] = time();
	}
	// 修改前
	protected function _before_update(&$data, $option)
	{
	}
	// 删除前
	protected function _before_delete($option)
	{
		if(is_array($option['where']['id']))
		{
			$this->error = '不支持批量删除';
			return FALSE;
		}
	}
	/************************************ 其他方法 ********************************************/

    public function region() {
        $regionId = I('get.id',1);
        $regionModel = M('Region');
        $regionData = $regionModel->where(array(
            'parent_id' => array('eq', $regionId),
        ))->select();
        echo json_encode($regionData);
    }
}